﻿using System;
using System.Collections.Generic;
using Caliburn.Micro;
using Caliburn.Micro.Recipes.Filters.Framework;
using SS.WPF.Framework.Display.Dialog;
using SS.WPF.Framework.ExceptionHandling;
using SS.WPF.Framework.Localization;
using SS.WPF.Framework.ViewModel.Navigation;
using SS.WPF.Framework.ViewModel.Screen;

namespace TestFramework.ViewModels
{
    using System.ComponentModel.Composition;

    [Export(typeof(ScreenTwoViewModel))]
    [Rescue("HandleException")]
    public class ScreenTwoViewModel : EditorViewModelBase<NavigationNode>
    {
        double _A;
        public double A
        {
            get { return _A; }
            set
            {
                _A = value;
                NotifyOfPropertyChange(() => A);
                NotifyOfPropertyChange(() => CanDivide);
            }
        }

        double _B;
        public double B
        {
            get { return _B; }
            set
            {
                _B = value;
                NotifyOfPropertyChange(() => B);
                NotifyOfPropertyChange(() => CanDivide);
            }
        }

        double _Result;
        public double Result
        {
            get { return _Result; }
            set { _Result = value; NotifyOfPropertyChange(() => Result); }
        }

        public bool CanDivide
        {
            get
            {
                return A != 0 && B != 0;
            }
        }


        public void Divide()
        {
            Result = A / B;

        }

        [Preview("CanDivide")]
        [Dependencies("A", "B")]
        public void VerySlowDivide()
        {
            Result = A / B;
        }

        public IEnumerable<IResult> ThrowingDivide()
        {
            yield return this.BuildLongRun()
                .Do(() =>
                {
                    try
                    {
                        var a = 1;
                        var b = 0;
                        var c = a/b;
                    }
                    catch (Exception ex)
                    {
                        throw new CustomException(ex, "DevidedByZero");
                    }
                })
                .OnException(ex =>
                {
                    if (ex is CustomException)
                    {
                        this.DialogManager.ShowErrorByKey(((CustomException) ex).MessageID);
                    }
                    else
                    {
                        this.DialogManager.ShowErrorByKey("RestartServiceError", ex.Message);
                    }
                })
                .BlockingUI().Title("Throw exception");

        }
    }
}
